package pro.lepers.portfolio.view.contents.portfolio.misc {
	import fr.digitas.flowearth.event.NumberEvent;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * @author plepers
	 */
	public class SeekBar extends Sprite {
		
		
		public var bar : MovieClip;
		
		public var bg : MovieClip;
		public var track : MovieClip;

		
		
		public function SeekBar () {
			addEventListener( MouseEvent.MOUSE_DOWN, startSeek );
			addEventListener( Event.ADDED_TO_STAGE, onAdded );
			addEventListener( Event.REMOVED_FROM_STAGE, onRemoved );
		}

		
		override public function set width (value : Number) : void {
			bg.width = track.width = value;
			bar.width = Math.max( _progress * track.width, 5 );
		}

		private function onAdded( e : Event ) : void {
		}
		
		private function onRemoved( e : Event ) : void {
			stage.removeEventListener( MouseEvent.MOUSE_UP, stopSeek );
			stage.removeEventListener( MouseEvent.MOUSE_MOVE, seekMove );
		}

		private function startSeek (event : MouseEvent) : void {
			event.stopPropagation();
			dispatchEvent( new Event( VideoControl.START_SEEK ) );
			stage.addEventListener( MouseEvent.MOUSE_UP, stopSeek );
			stage.addEventListener( MouseEvent.MOUSE_MOVE, seekMove );
			seekMove( null );
		}

		private function seekMove (event : MouseEvent) : void {
			var prog : Number = mouseX / track.width;
			prog = Math.min( prog, 1 );
			prog = Math.max( prog, 0 );
			progress = prog;
			dispatchEvent( new NumberEvent( VideoControl.SEEK, prog ) );
		}

		private function stopSeek (event : MouseEvent) : void {
			dispatchEvent( new Event( VideoControl.STOP_SEEK ) );
			stage.removeEventListener( MouseEvent.MOUSE_MOVE, seekMove );
			
		}

		private var _progress : Number = 0;
		
		public function get progress () : Number {
			return _progress;
		}
		
		public function set progress (progress : Number) : void {
			_progress = progress;
			bar.width = Math.max( _progress * track.width, 5 );
		}
	}
}
